package com.relytosoft.mizar.media.camera.services.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.mizar.media.camera.api.entity.CameraChannelOsd;
import com.relytosoft.mizar.media.camera.api.entity.CameraPlatform;
import com.relytosoft.mizar.media.camera.services.mapper.CameraChannelOsdMapper;
import com.relytosoft.mizar.media.camera.services.service.CameraChannelOsdService;
import com.relytosoft.mizar.media.camera.services.service.CameraPlatformService;
import com.relytosoft.mizar.media.external.api.camera.response.OSDDto;
import com.relytosoft.mizar.media.external.api.camera.response.PreviewFrameDto;
import com.relytosoft.mizar.media.external.api.camera.response.ShowStringParams;
import com.relytosoft.mizar.media.external.api.camera.response.VideoStringDto;
import com.relytosoft.mizar.media.external.api.camera.service.OSDService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * @author a3621
 */
@Service
public class CameraChannelOsdServiceImpl extends ServiceImpl<CameraChannelOsdMapper, CameraChannelOsd> implements CameraChannelOsdService {

    @Resource
    private OSDService osdService;

    @Resource
    private CameraPlatformService cameraPlatformService;


    @Override
    public OSDDto getOsdInfo(String cameraIndexCode) {
        OSDDto osdInfo = osdService.findOsdInfo(cameraIndexCode);
        addOsdInfo(osdInfo);
        return osdInfo;
    }

    @Override
    public VideoStringDto showVideoString(String cameraIndexCode) {
        return osdService.findOverlayString(cameraIndexCode);
    }

    @Override
    public PreviewFrameDto previewFrame(String cameraIndexCode) {
        return osdService.findPreviewFreame(cameraIndexCode);
    }

    @Override
    public boolean setOsd(OSDDto osddto) {
        return osdService.setOsdInfo(osddto);

    }


    @Override
    public boolean setOverlayString(String cameraIndexCode, List<ShowStringParams> showStringParamsList) {
        return osdService.setOverlayString(cameraIndexCode, showStringParamsList);
    }

    @Override
    public boolean setPreview(PreviewFrameDto previewFrameDTO) {
        return osdService.setPreviewFreame(previewFrameDTO);
    }


    public void addOsdInfo(OSDDto osdInfo) {
        //1. 查询
        LambdaQueryWrapper<CameraPlatform> wrapper = Wrappers.lambdaQuery();
        CameraPlatform cameraPlatform = cameraPlatformService.getOne(wrapper.eq(!StrUtil.isEmptyIfStr(osdInfo.getCameraIndexCode()), CameraPlatform::getPlatformChannelCode, osdInfo.getCameraIndexCode()));
        if (cameraPlatform != null) {
            CameraChannelOsd cameraChannelOsd = this.getOne(
                    new QueryWrapper<CameraChannelOsd>().lambda()
                            .eq(CameraChannelOsd::getChannelId, cameraPlatform.getOwnId()));
            if (cameraChannelOsd == null) {
                CameraChannelOsd channelOsd = new CameraChannelOsd();

                channelOsd.setChannelId(cameraPlatform.getOwnId());

                channelOsd.setIsEnable(osdInfo.getIsShowOSD());

                channelOsd.setIsShowtime(osdInfo.getOsdType());

                channelOsd.setTimeX(BigDecimal.valueOf(osdInfo.getOsdXPos()));

                channelOsd.setTimeY(BigDecimal.valueOf(osdInfo.getOsdYPos()));

                channelOsd.setIsShowname(osdInfo.getIsShowChanName());

                channelOsd.setName(osdInfo.getChannelName());

                channelOsd.setNameX(BigDecimal.valueOf(osdInfo.getChannelNameXPos()));

                channelOsd.setNameY(BigDecimal.valueOf(osdInfo.getChannelNameYPos()));

                this.save(channelOsd);
            }
        }
    }
}
